# -*- coding: utf-8 -*-
"""
@Time : 2024/4/9 14:04
@Author : ChenXiaoliang
@Email : middlegod@sina.com
@File : login_jd.py
"""
import random
import time

from selenium import webdriver
from selenium.webdriver import ActionChains

from slide_crypto import get_bg_slide_img, get_distance, get_slide_track

driver_path = "D:\webdriver\chromedriver.exe"
driver = webdriver.Chrome(driver_path)
driver.get("https://passport.jd.com/new/login.aspx")
driver.maximize_window()
login_box = driver.find_element_by_class_name("login-box")
login_form = login_box.find_element_by_class_name("form")
username = login_form.find_element_by_class_name("item-fore1").find_element_by_class_name("itxt")
username.send_keys("136713xxxxx")
time.sleep(1)
pw = login_form.find_element_by_class_name("item-fore2").find_element_by_class_name("itxt")
pw.send_keys("@#$%^%&^^%")
time.sleep(1)
submit = login_form.find_element_by_class_name("item-fore5").find_element_by_class_name("btn-entry")
submit.click()
time.sleep(5)
# time.sleep(5000)

# 处理滑动验证
get_bg_slide_img(driver)
slider = driver.find_element_by_class_name("JDJRV-slide-btn")
# 获取滑动距离
dis = get_distance()
print("滑动距离：", dis)
time.sleep(1)
# 执行滑块滑动动作
act = ActionChains(driver)
act.click_and_hold(slider).perform()
time.sleep(1)
for i in get_slide_track(dis):
    print(f"滑动距离：{i}")
    act.move_by_offset(i, random.randrange(1, 3))
act.release().perform()
